#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

#include "soft_dev.h"
#include "trace.h"


static uint32_t write_data_index = 0, _base_count = 0, _base_line = 1;
static uint8_t index_dbg =0;
static FILE *fp = NULL;
int32_t config_manager_fetch_info(device_info_config_t *cfg);

static int32_t _init(void * platform_dev, bool tFlage, uint8_t pannel_mode)
{
	virt_printf(" Func: %s, %d \n", __func__, __LINE__);
	return 0;
}
static int32_t _write_cmd(void * platform_dev, const uint8_t cmd)
{
	virt_printf("\nFunc: %s, %d, CMD:0x%02x \n\r", __func__, __LINE__, cmd);
	write_data_index = 0;
	_base_line = 1;
	if(cmd == 0x10){
		fp = fopen("./out_10.bin","wb");
	} else if(cmd == 0x13){
		close("./out_10.bin");
		fp = fopen("./out_13.bin","wb");
	}
	return 0;
}

static int32_t _write_data(void * platform_dev, const uint8_t *data, uint32_t len)
{
	d_printf("%02x ",*data);
	fwrite(data, 1, 1, fp);
	write_data_index += 1;
	_base_count++;

	return 0;
}

static int32_t _refresh(void * platform_dev)
{
	close("./out_13.bin");
	virt_printf("\nDot: %d \n\r", _base_count++);
	return 0;
}

sse320_handle_t virt_create_dev(spi_host_device_t spi_host, e_eink_size size, uint8_t mode)
{

	device_info_config_t *dev = (device_info_config_t*)calloc(1,sizeof(device_info_config_t));
	config_manager_fetch_info(dev);

	dev_soft_drv *drv = (dev_soft_drv*)calloc(1,sizeof(dev_soft_drv));
	drv->eink_init = _init;
	drv->eink_deinit = NULL;
	drv->cmd = _write_cmd;
	drv->data = _write_data;
	drv->refresh = _refresh;

	virt_dev_drv *platform_dev = (virt_dev_drv *)calloc(1,sizeof(virt_dev_drv));
	platform_dev->disp_mode = mode;
	platform_dev->dev = dev;
	platform_dev->drv = drv;
	virt_printf(" Func: %s, %d \n", __func__, __LINE__);
	return platform_dev;
}

esp_err_t virt_delete_dev(sse320_handle_t dev)
{
	virt_dev_drv *platform_dev = (virt_dev_drv *)dev;
	esp_err_t ret = 0;
	free(platform_dev->dev);
	free(platform_dev->drv);
	free(platform_dev);
	return ret;
}

int32_t config_manager_fetch_info(device_info_config_t *cfg)
{
	int32_t err;
	memset(cfg, 0, sizeof(device_info_config_t));

	memcpy(cfg->company, DEFAULT_COMPANY, strlen(DEFAULT_COMPANY));
	memcpy(cfg->serial, DEFAULT_SERIAL_NUMMBER, strlen(DEFAULT_SERIAL_NUMMBER));

	cfg->model.type = DEFAULT_MODEL;
	cfg->model.color = DEFAULT_COLOR;
	cfg->model.width = DEFAULT_MODEL_WIDTH;
	cfg->model.height = DEFAULT_MODEL_HEIGHT;

	cfg->hw.major = DEFAULT_HW_VER_MAJOR;
	cfg->hw.minor = DEFAULT_HW_VER_MINOR;

	cfg->soft.major = DEFAULT_SOFT_VER_MAJOR;
	cfg->soft.minor = DEFAULT_SOFT_VER_MINOR;
	cfg->soft.patch = DEFAULT_SOFT_VER_PATCH;

	virt_printf( "Device Info:");
	virt_printf( "    Company: '%s'\n", cfg->company);
	virt_printf( "    Model: '%s'\n", (cfg->model.type == MODEL_213) ? "Mink213" : "Mink750");
	virt_printf( "    Color: '%s'\n", (cfg->model.color == COLOR_MONOCHROME) ? "Monochrome" : "Tricolor");
	virt_printf( "    Serial: '%s'\n", cfg->serial);
	virt_printf( "    Hardware Version: %u.%u\n", cfg->hw.major, cfg->hw.major);
	virt_printf( "    Software Version: %s \n", SOFT_VER_STRING);

err_exit:

	return err;
}
